const { uniacid } = require("../../../siteinfo");

var _extends = Object.assign || function(t) {
    for (var e = 1; e < arguments.length; e++) {
        var a, s = arguments[e];
        for (a in s) Object.prototype.hasOwnProperty.call(s, a) && (t[a] = s[a]);
    }
    return t;
}, _tools = require("../../../utils/tools");

function _defineProperty(t, e, a) {
    return e in t ? Object.defineProperty(t, e, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = a, t;
}

var g, city, app = getApp(), page = app.page, request = app.util.request, route = app.route;

function lawPay(t, e, a) {
    
    request({
        url: "LawInPay",
        data: {
            user_id: app.userInfo.id || wx.getStorageSync("userInfo").id,
            order_id: t
        },
        success: function(t) {
            t.appId ? (0, _tools.wxPay)({
                data: t,
                success: function(t) {
                    wx.setStorageSync('applyData', '')
                    post(e, a);
                }
            }) : toast("错误:" + t);
        }
    });
}

function post(t, e) {
    wx.showLoading({
        title: "上传图片中...",
        mask: !0
    }), (0, _tools.uploadImg)(t, function(t) {
        t = t.split(","), wx.showLoading({
            title: "提交中...",
            mask: !0
        }), request({
            url: "LawSettled",
            data: _extends({}, e, {
                district_id: 0,
                img: t[0],
                sfz_zheng: "2" == g.data.system.sfz_state ? t[1] : "",
                sfz_fan: "2" == g.data.system.sfz_state ? t[2] : "",
                zyz_img: "2" == g.data.system.sfz_state ? t[3] || "" : t[1] || ""
            }),
            method: "POST",
            showLoading: !1,
            success: function(t) {
                wx.hideLoading(), 1 == t ? (app.userInfo.is_law = 3, app.userInfo.sh_state = 0, 
                wx.setStorageSync("userInfo", app.userInfo), app.item = {
                    title: "提交成功",
                    content: "入驻申请提交成功",
                    img: "pay_ok",
                    button: [ {
                        title: "返回首页",
                        url: "loading",
                        type: "reLaunch"
                    } ]
                }, route({
                    type: "redirect",
                    url: "result"
                })) : toast("错误:" + t);
            }
        });
    });
}

function toast(t) {
    wx.showToast({
        icon: "none",
        title: t
    });
}

function getLocation() {
    request({
        url: "Location",
        success: function(t) {
            t = t.province;
            var e, a = [], s = [];
            city = [];
            for (var o = 0; o < t.length; o++) {
                e = t[o], s[o] = {
                    id: e.id,
                    name: e.name
                }, e = e.city, city[o] = [];
                for (var i = 0; i < e.length; i++) city[o].push({
                    id: e[i].id,
                    name: e[i].name
                });
            }
            a.push(s), a.push(city[0]), wx.setStorageSync("location", a), wx.setStorageSync("city", city), 
            g.setData({
                address: a
            });
        }
    });
}

page({
    data: {
        src: app.srcRoot,
        sex: wx.getStorageSync("userInfo").gender,
        addressIndx: [ 0, 0 ],
        deadlineIndex: 0,
        date: (0, _tools.getDate)(),
        radio: !1,
        dh_state: !0,
        img_state: !0,
        jslt_state: !0,
        Modalshow: !1,
        applyForm: {}
    },
    onLoad: function(t) {
        g = this, app.lawContent = "";
        let applyData = wx.getStorageSync('applyData')
        if(applyData){
            delete applyData.system
            g.setData({
                ...applyData
            });
            app.item = applyData.goodareas
            app.lawContent = applyData.lawContent
        } else {
            var e = wx.getStorageSync("location");
            city = wx.getStorageSync("city"), e && city ? g.setData({
                address: e
            }) : getLocation(), request({
                url: "GetInTerm",
                showLoading: !1,
                success: function(t) {
                    g.setData({
                        deadline: t
                    });
                }
            });
        }
    },
    switchChange: function(t) {
        var e = "dh_state";
        switch (t.currentTarget.dataset.i) {
          case "1":
            e = "img_state";
            break;

          case "2":
            e = "jslt_state";
        }
        g.setData(_defineProperty({}, e, t.detail.value));
    },
    onShow: function() {
        (0, _tools.verifyLogin)({});
        var t = [];
        if (app.item) for (var e = 0; e < app.item.length; e++) app.item[e].checked && t.push(app.item[e]);
        g.setData({
            goodareas: t
        });
    },
    addressColumnChange: function(t) {
        var e = (t = t.detail).column, t = t.value;
        0 === e && g.setData({
            "address[1]": city[t]
        });
    },
    Change: function(t) {
        var e = t.currentTarget.dataset.i;
        g.setData(_defineProperty({}, e, t.detail.value));
    },
    toDomain: function() {
        route({
            type: "navigate",
            url: "domain"
        });
    },
    ChooseImage: function(t) {
        var e, a = parseInt(t.currentTarget.dataset.type);
        wx.chooseImage({
            count: 1,
            success: function(t) {
                switch (a) {
                  case 0:
                    e = "img";
                    break;

                  case 1:
                    e = "sfz_zheng";
                    break;

                  case 2:
                    e = "sfz_fan";
                    break;

                  case 3:
                    e = "zyz_photo";
                }
                g.setData(_defineProperty({}, e, t.tempFilePaths[0]));
            }
        });
    },
    radio: function(t) {
        g.setData({
            radio: !g.data.radio
        });
    },
    html_page: function() {
        app.centent = g.data.system.law_xieyi, route({
            type: "navigate",
            url: "html_page",
            params: {
                title: "入驻协议"
            }
        });
    },
    shxx: function() {
        g.setData({
            isStorage : !1
         });
        (0, _tools.subscribe)("ENTER");
    },
    saveStorage: function() {
        g.setData({
           isStorage : !0
        });
    },
    submit: function(t) {
        t = t.detail.value;
        if(g.data.isStorage){
            g.data.applyForm = t
            g.data.lawContent = app.lawContent
            wx.setStorageSync('applyData', g.data)
            toast("已暂存")
            return false
        }
        for (var e, a, s, o = g.data, i = t.name, n = t.phone, d = t.sex ? 1 : 2, r = t.lawfirm, c = t.licenseno || "", u = o.date, l = o.dh_state && 1 != g.data.system.is_dianhua ? t.dh_price : "0.00", p = o.img_state && 1 != g.data.system.is_tuwen ? t.img_price : "0.00", _ = o.jslt_state && 1 != g.data.system.is_jslt ? t.jslt_price : "0.00", f = t.dh_state ? 2 : 1, y = t.img_state ? 2 : 1, m = t.jslt_state ? 2 : 1, h = app.lawContent, w = o.img, x = o.sfz_zheng, z = o.sfz_fan, v = o.zyz_photo, I = "", S = t.dh_unit || "单", L = t.img_unit || "单", D = t.jslt_unit || "单", b = 0; b < o.goodareas.length; b++) I += (0 != b ? "," : "") + o.goodareas[b].id;
        if (e = o.address[0][o.addressIndx[0]].id, a = o.address[1][o.addressIndx[1]].id, 
        i) if (n) if (r) if (c || "0" == g.data.system.show_zyz_num) if (o.chooseLocation) if (I) if (l) if (p) if (_) if (w) {
            if ("2" == this.data.system.zyz_state && !v) return toast("请上传" + (this.data.system.zyz_title || "执业证照片"));
            "2" != this.data.system.sfz_state || x && z ? o.radio ? (t = o.deadline[o.deadlineIndex], 
            s = _extends({
                user_id: app.userInfo.id || wx.getStorageSync("userInfo").id,
                name: i,
                phone: n,
                sex: d,
                lawfirm: r,
                licenseno: c,
                work_year: u,
                dh_price: l,
                img_price: p,
                jslt_price: _,
                goodareas_id: I,
                province_id: e,
                city_id: a,
                content: h,
                dh_state: f,
                img_state: y,
                jslt_state: m,
                daoqi_time: (0, _tools.getTimestamp)((0, _tools.getDate)(), (0, _tools.getTime)()) + 86400 * t.in_day,
                dh_unit: S,
                img_unit: L,
                jslt_unit: D
            }, o.chooseLocation), w = [ w ], "2" == this.data.system.zyz_state && w.push(v), 
            "2" == this.data.system.sfz_state && (w.push(x), w.push(z)), 2 == t.is_pay ? o.order_id ? lawPay(o.order_id, w, s) : request({
                url: "LawInOrder",
                data: {
                    user_id: app.userInfo.id || wx.getStorageSync("userInfo").id,
                    money: t.in_money,
                    rz_time: t.in_day
                },
                method: "POST",
                success: function(t) {
                    t.id ? (lawPay(t.id, w, s), g.setData({
                        order_id: t.id
                    })) : toast("错误:" + t);
                }
            }) : post(w, s)) : toast("请勾选" + g.data.system.law_custom + "协议") : toast("请上传身份证");
        } else toast("请上传头像"); else toast("请填写" + g.data.system.jslt_title + "价格"); else toast("请填写" + g.data.system.tuwen_title + "价格"); else toast("请填写" + g.data.system.dianhua_title + "价格"); else toast("请选择擅长领域"); else toast("请选择所在位置"); else toast("请填写" + g.data.system.zhiye + "证号"); else toast("请填写" + g.data.system.ssls + "名称"); else toast("请填写输入手机号码"); else toast("请填写姓名");
    },
    getAddress: function() {
        wx.chooseLocation({
            success: function(t) {
                g.setData({
                    chooseLocation: {
                        address: t.address,
                        longitude: t.longitude,
                        latitude: t.latitude
                    }
                });
            }
        });
    },
    hideModal: function() {
        g.setData({
            Modalshow: !g.data.Modalshow
        });
    },
    toEditor: function() {
        app.lawContent = app.lawContent || "", route({
            url: "editor"
        });
    }
});